package com.lzy.pulltorefreshswipemenulistview;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Toast;

import com.lzy.pulltorefreshswipemenulistview.refreshswipelistview.pulltolistview.RefreshTime;
import com.lzy.pulltorefreshswipemenulistview.adapter.MyAdapter;
import com.lzy.pulltorefreshswipemenulistview.bean.AnyBean;
import com.lzy.pulltorefreshswipemenulistview.refreshswipelistview.PullToRefreshSwipeMenuListView;
import com.lzy.pulltorefreshswipemenulistview.refreshswipelistview.PullToRefreshSwipeMenuListView.IXListViewListener;
import com.lzy.pulltorefreshswipemenulistview.refreshswipelistview.PullToRefreshSwipeMenuListView.OnMenuItemClickListener;
import com.lzy.pulltorefreshswipemenulistview.refreshswipelistview.swipemenulistview.SwipeMenu;
import com.lzy.pulltorefreshswipemenulistview.refreshswipelistview.swipemenulistview.SwipeMenuCreator;
import com.lzy.pulltorefreshswipemenulistview.refreshswipelistview.swipemenulistview.SwipeMenuItem;
import com.lzy.pulltorefreshswipemenulistview.util.DensityUtils;

/**
 * 类描述：测试类
 * 
 * @Time 2016年4月21日
 * @author lizy18
 */
public class MainActivity extends Activity {

	private PullToRefreshSwipeMenuListView listview;
	private int count;
	private List<AnyBean> mList;
	private Handler mHandler;
	private MyAdapter myAdapter;
	private SwipeMenuCreator creator;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initViews();
		initVars();
		loadData();
		myAdapter = new MyAdapter(MainActivity.this, mList);
		listview.setAdapter(myAdapter);

		initPullToRefreshSwipeMenuListView();
	}

	/**
	 * 功能描述：初始化initPullToRefreshSwipeMenuListView
	 * 
	 * @Time 2016年4月21日
	 * @Author lizy18
	 */
	private void initPullToRefreshSwipeMenuListView() {
		initRefreshListView();
		initSwipeMenuListView();
		initListViewListener();
	}

	/**
	 * 功能描述：初始化ListView的监听事件
	 * 
	 * @Time 2016年4月21日
	 * @Author lizy18
	 */
	private void initListViewListener() {
		// listview的点击监听事件
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(MainActivity.this,
						" 您点击了：" + position + ",ID = " + id, Toast.LENGTH_SHORT)
						.show();

			}
		});
		// listview的长点击监听事件
		listview.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getApplicationContext(),
						position + " long click", Toast.LENGTH_SHORT).show();
				return false;
			}
		});
	}

	/**
	 * 功能描述：初始化ListView的item中侧滑菜单（仿QQ）
	 * 
	 * @Time 2016年4月21日
	 * @Author lizy18
	 */
	private void initSwipeMenuListView() {
		// 创建一个SwipeMenu的生成器
		creator = new SwipeMenuCreator() {

			@Override
			public void create(SwipeMenu menu) {
				createMenuItem(menu);
			}
		};
		listview.setMenuCreator(creator);
		// 为菜单设置监听事件
		listview.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public void onMenuItemClick(int position, SwipeMenu menu, int index) {
				// AnyBean item = mList.get(position);
				switch (index) {
				case 0:
					Toast.makeText(MainActivity.this, "Open" + index,
							Toast.LENGTH_SHORT).show();
					break;
				case 1:
					Toast.makeText(MainActivity.this, "点击了标记为已读" + index,
							Toast.LENGTH_SHORT).show();
					break;
				case 2:
					Toast.makeText(MainActivity.this, "d点击了删除" + index,
							Toast.LENGTH_SHORT).show();
					break;
				default:
					break;
				}
			}
		});
	}

	/**
	 * 功能描述：初始化ListView的刷新和加载更多的配置
	 * 
	 * @Time 2016年4月21日
	 * @Author lizy18
	 */
	private void initRefreshListView() {
		listview.setPullRefreshEnable(true);
		listview.setPullLoadEnable(true);
		listview.setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
				mHandler.postDelayed(new Runnable() {
					@Override
					public void run() {
						refreshData();
					}
				}, 2000);
			}

			@Override
			public void onLoadMore() {
				mHandler.postDelayed(new Runnable() {
					@Override
					public void run() {
						refreshData();
					}
				}, 2000);

			}
		});
	}

	/**
	 * 功能描述：初始化布局控件
	 * 
	 * @Time 2016年4月21日
	 * @Author lizy18
	 */
	private void initViews() {
		listview = (PullToRefreshSwipeMenuListView) findViewById(R.id.ptrsmlv);
	}

	/**
	 * 功能描述：创建并配置SwipeMenu的item--》SwipeMenuItem
	 * 
	 * @Time 2016年4月21日
	 * @Author lizy18
	 * @param menu
	 */
	private void createMenuItem(SwipeMenu menu) {
		// create "open" item(创建一个菜单条)
		SwipeMenuItem openItem = new SwipeMenuItem(getApplicationContext());
		// set item background(设置菜单的背景)
		openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE)));
		// set item width(设置菜单宽度，菜单的宽度是一定要有的，否则不显示菜单)
		openItem.setWidth(DensityUtils.dpToPx(MainActivity.this, 90));
		// set item title(设置菜单标题)
		openItem.setTitle("Open");
		// set item title fontsize(设置菜单标题字体大小)
		openItem.setTitleSize(18);
		// set item title font color(设置菜单标题字体的颜色)
		openItem.setTitleColor(Color.WHITE);
		// add to menu(将刚刚创建的菜单条添加到menu)
		menu.addMenuItem(openItem);

		// create "read" item
		final SwipeMenuItem readItem = new SwipeMenuItem(
				getApplicationContext());
		readItem.setBackground(new ColorDrawable(Color.rgb(0xFF, 0x7E, 0x00)));
		readItem.setWidth(DensityUtils.dpToPx(MainActivity.this, 90));
		readItem.setTitle("标记为已读");
		readItem.setTitleSize(16);
		readItem.setTitleColor(Color.WHITE);
		menu.addMenuItem(readItem);

		// create "delete" item
		SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());
		// set item background
		deleteItem
				.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
		// set item width
		deleteItem.setWidth(DensityUtils.dpToPx(MainActivity.this, 90));
		// set a icon
		deleteItem.setIcon(R.drawable.ic_delete);
		// add to menu
		menu.addMenuItem(deleteItem);
	}

	/**
	 * 功能描述：初始化变量信息
	 * 
	 * @Time 2016年4月21日
	 * @Author lizy18
	 */
	private void initVars() {
		count = 0;
		mList = new ArrayList<AnyBean>();
		mHandler = new Handler();
	}

	/**
	 * 功能描述：刷新（加载更多）操作时加载数据
	 * 
	 * @Time 2016年4月21日
	 * @Author lizy18
	 */
	private void refreshData() {
		SimpleDateFormat df = new SimpleDateFormat("MM-dd HH:mm",
				Locale.getDefault());
		RefreshTime.setRefreshTime(getApplicationContext(),
				df.format(new Date()));
		listview.setRefreshTime(RefreshTime
				.getRefreshTime(getApplicationContext()));
		listview.stopRefresh();
		listview.stopLoadMore();
		loadData();
		myAdapter.notifyDataSetChanged();
	}

	/**
	 * 功能描述：模拟假数据
	 * 
	 * @Time 2016年4月21日
	 * @Author lizy18
	 */
	private void loadData() {
		for (int i = 0; i < 15; i++) {
			AnyBean bean = new AnyBean();
			bean.setTxt("第" + count + "条数据："
					+ "PullToRefresh是一个非常完美的下拉刷新的开源项目，"
					+ "SwipeMenuListView是一个向左滑动ListView中item实现可以删除功能的开源项目。"
					+ "笔者在此将两套开源项目整合形成一套，类似于手机qq那样同时支持下拉刷新和向左滑动的列表"
					+ "Caused by: android.os.TransactionTooLargeException是"
					+ "传递参数太大造成的。");
			count++;
			mList.add(bean);
		}
	}
}
